<script setup>
import {ref} from 'vue'
import PageContainer from '../../components/PageContainer.vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { $roledelapi, $rolelistapi } from '@/api/roleApi';
import RoleEdit from '@/components/RoleEdit.vue';
const channellist=ref([])
const loadding = ref(false)
const dialog = ref()
const roleIdRef = ref({
    roleId:''
})
const getChannelList=()=>{
    loadding.value = true
    const res=$rolelistapi().then(data=>{
        
        data=data.data.map(r=>{
            return {
                ...r,
                key:r.roleId
            }
        })
        console.log(data)
        channellist.value=data
        
    })
    loadding.value = false
    // channellist.value=res
    // console.log(channellist.value)
}
getChannelList()
const handleEdit = (row) => {
  dialog.value.open(row)
}
const handleDelete = async (row) => {
  await ElMessageBox.confirm('你确定删除吗', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  roleIdRef.value={...row}
  $roledelapi(roleIdRef.value).then(data=>{
    
    if(data.data.success){
        ElMessage({ type: 'success', message: '删除成功' })
        getChannelList()
    }else{
        ElMessage({ type: 'fail', message: message })
    }
  })
 
}
const onAddChannel = () => {
  dialog.value.open({})
  title.value = '添加分类'
}
const onSuccess = () => {
    getChannelList()
}
</script>
<template>
    <page-container title="角色管理">
        <template #extra>
            <el-button type="primary" @click="onAddChannel">添加</el-button>
        </template>
        <el-table v-loadding="loadding" :data="channellist" style="width: 100%;">
            <el-table-column prop="roleId" label="角色编号"></el-table-column>
            <el-table-column prop="roleName" label="角色名称"></el-table-column>
            <el-table-column label="操作">
                <template #default="{ row }">
            <el-button
              type="primary"
              :icon="Edit"
              circle
              plain
              size="small"
              @click="handleEdit(row)"
            ></el-button>
            <el-button
              plain
              size="small"
              type="danger"
              :icon="Delete"
              circle
              @click="handleDelete(row)"
            ></el-button>
          </template>
            </el-table-column>
            <template #empty>
          <el-empty description="没有数据" />
        </template>
        </el-table>
        <RoleEdit ref="dialog" @success="onSuccess"></RoleEdit>
    </page-container>
</template>
<style lang="scss" scoped></style>